﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class CatalogItemTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [Test]
        public void Can_CRUD()
        {
            var item = Processor.Execute(new CreateCatalogItem
            {
                FileGroupID = 0,
                SupplierID = Processor.Execute(new CreateCatalogSupplier()).Result.EntityID
            }).Result;

            Assert.NotNull(item);
            item.AreEqual(Processor.Execute(new GetCatalogItem
            {
                EntityID = item.EntityID
            }).Result);

            var upd_item = Processor.Execute(new UpdateCatalogItem
            {
                EntityID = item.EntityID,
                SupplierID = item.Supplier.EntityID,
                FileGroupID = 0,
                Flags = CatalogItemFlags.Active
            }).Result;

            Assert.NotNull(upd_item);
            upd_item.AreEqual(item);
            upd_item.Flags.AreEqual(CatalogItemFlags.Active);

            Processor.Execute(new DeleteCatalogItem
            {
                EntityID = item.EntityID
            });
        }

        [Test]
        public void Can_Manage_Tag_Relations()
        {
            var tag = Processor.Execute(new CreateCatalogTag()).Result;
            var item = Processor.Execute(new CreateCatalogItem
            {
                SupplierID = Processor.Execute(new CreateCatalogSupplier()).Result.EntityID,
                FileGroupID = 0
            }).Result;

            Processor.Execute(new MapCatalogTagToCatalogItem
            {
                ChildID = tag.EntityID,
                EntityID = item.EntityID
            }).Result.Tags
            .DoesContain(tag);

            Processor.Execute(new UnmapCatalogTagFromCatalogItem
            {
                ChildID = tag.EntityID,
                EntityID = item.EntityID
            }).Result.Tags
            .DoesNotContain(tag);
        }

        [Test]
        public void Can_Manage_Category_Relations()
        {
            var category = Processor.Execute(new CreateCatalogCategory()).Result;
            var item = Processor.Execute(new CreateCatalogItem
            {
                SupplierID = Processor.Execute(new CreateCatalogSupplier()).Result.EntityID,
                FileGroupID = 0
            }).Result;

            Processor.Execute(new MapCatalogCategoryToCatalogItem
            {
                ChildID = category.EntityID,
                EntityID = item.EntityID
            }).Result.Categories
                .DoesContain(category);

            Processor.Execute(new UnmapCatalogCategoryFromCatalogItem
            {
                ChildID = category.EntityID,
                EntityID = item.EntityID
            }).Result.Categories
                .DoesNotContain(category);
        }

        [Test]
        public void Can_Manage_Attribute_Relations()
        {
            var attribute = Processor.Execute(new CreateLookupAttribute
            {
                Flags = LookupAttributeFlags.CatalogItem
            }).Result;
            var item = Processor.Execute(new CreateCatalogItem
            {
                SupplierID = Processor.Execute(new CreateCatalogSupplier()).Result.EntityID,
                FileGroupID = 0
            }).Result;

            Processor.Execute(new MapLookupAttributeToCatalogItem
            {
                ChildID = attribute.EntityID,
                EntityID = item.EntityID
            }).Result.Attributes
                .DoesContain(attribute);

            Processor.Execute(new UnmapLookupAttributeFromCatalogItem
            {
                ChildID = attribute.EntityID,
                EntityID = item.EntityID
            }).Result.Attributes
                .DoesNotContain(attribute);
        }

        [Test]
        public void Can_Manage_Group_Relations()
        {
            var supplier = Processor.Execute(new CreateCatalogSupplier()).Result;
            var group = Processor.Execute(new CreateCatalogItemGroup
            {
                SupplierID = supplier.EntityID
            }).Result;
            var item = Processor.Execute(new CreateCatalogItem
            {
                SupplierID = supplier.EntityID,
                FileGroupID = 0
            }).Result;

            Processor.Execute(new MapCatalogItemGroupToCatalogItem
            {
                ChildID = group.EntityID,
                EntityID = item.EntityID
            }).Result.Groups
                .DoesContain(group);

            Processor.Execute(new UnmapCatalogItemGroupFromCatalogItem
            {
                ChildID = group.EntityID,
                EntityID = item.EntityID
            }).Result.Groups
                .DoesNotContain(group);
        }
    }
}
